<template>
  <div class="home">
    <el-container class="container">
      <el-header>
        <div class="logo">
          <img src="../assets/logo.png" />
          <span>电商后台管理系统</span>
        </div>
        <!-- 右侧登录按钮 -->
        <el-row>
          <el-button type="info" @click="logout">退出</el-button>
        </el-row>
      </el-header>
      <el-container>
        <el-aside :style="collapse == false? 'width:200px':'width:64px'">
          <div
            class="toggle_bar"
            :style="collapse == false? 'width:200px':'width:64px'"
            @click="toggleBar"
          >|||</div>
          <!-- 左侧菜单 -->
          <el-menu
            collapse-transition
            :collapse="collapse"
            :router="true"
            :unique-opened="true"
            background-color="#333744"
            text-color="#fff"
            active-text-color="#ffd04b"
          >
            <el-submenu
              :index="index+''"
              :default-active="index+''"
              v-for="(item,index) in menusList"
              :key="item.id"
            >
              <template slot="title">
                <i :class="iconList[index]"></i>
                <span>{{item.authName}}</span>
              </template>
              <el-menu-item
                :index="'/home/'+item2.path"
                v-for="item2 in item.children"
                :key="item2.id"
              >
                <i class="el-icon-menu"></i>
                <span slot="title">{{item2.authName}}</span>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 左边菜单栏数据
      menusList: [],
      // menu的展开折叠
      collapse: false,
      iconList: [
        "el-icon-user-solid",
        "el-icon-s-promotion",
        "el-icon-s-goods",
        "el-icon-s-order",
        "el-icon-s-marketing"
      ]
    };
  },
  created() {
    this.getMenusList();
  },
  methods: {
    // 获取左侧菜单栏数据
    async getMenusList() {
      let { data: res } = await this.$http.get("menus");
      // console.log(res);
      if (res.meta.status != 200)
        return this.$message.error("获取左侧菜单栏失败");
      this.menusList = res.data;
    },
    // 退出
    logout() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    // menu的展开折叠
    toggleBar() {
      this.collapse = !this.collapse;
    }
  }
};
</script>
<style lang="less" scoped>
.home {
  height: 100%;
}
.container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
}
.el-aside {
  background-color: #333744;
  transition: all 0.5s;
}
.logo {
  img {
    float: left;
    width: 50px;
    height: 50px;
    border: 1px solid #ffffff;
    border-radius: 50%;
    background-color: #ffffff;
    box-sizing: border-box;
    margin-top: 5px;
  }
  span {
    float: left;
    font-size: 22px;
    color: #ffffff;
    margin-left: 10px;
    margin-top: 15px;
  }
}
.el-row {
  float: right;
  margin-top: 10px;
}
.toggle_bar {
  width: 200px;
  color: #fff;
  font-size: 12px;
  text-align: center;
  line-height: 25px;
  background-color: #4a5064;
  user-select: none;
  cursor: pointer;
  letter-spacing: 0.1em;
  transition: all 0.5s;
}
.el-menu {
  border: none;
}
</style>
